<layout name='layout' />

<div class="admin-main">
	
	<div class="node">
		<li>
			<fieldset class="layui-elem-field">
				<legend>分组</legend>
				<div class="layui-field-box">
					<select name="group" id="group" class="node-select" SIZE="20">
						<volist name="node" id="vo">
						<option value="<{$vo.id}>"><{$vo.title}>(<{$vo.name}>)</option>
						</volist>
					</select>
					<p>
						<button class="layui-btn layui-btn-small" onclick="addNode( '<{:U('Node/add',array('level'=>1,'pid'=>0,'o'=>'group'))}>' , 'group' ,'添加分组')">添加</button>
						<button class="layui-btn layui-btn-small" onclick="editNode( '<{:U('Node/edit',array('o'=>'group'))}>' , 'group' , '编辑分组')">编辑</button>
						<button class="layui-btn layui-btn-small" onclick="delNode(1)">删除</button>
					</p>
				</div>
			</fieldset>
		</li>

		<li>
			<fieldset class="layui-elem-field">
				<legend>控制器</legend>
				<div class="layui-field-box">
					<select name="model" id="model" class="node-select" SIZE="10"></select>
					<p>
						<button class="layui-btn layui-btn-small" onclick="addNode( '<{:U('Node/add',array('level'=>2,'o'=>'model'))}>' , 'model' ,'添加分组')">添加</button>
						<button class="layui-btn layui-btn-small" onclick="editNode( '<{:U('Node/edit',array('o'=>'model'))}>' , 'model' , '编辑控制器')">编辑</button>
						<button class="layui-btn layui-btn-small" onclick="delNode(2)">删除</button>
					</p>
				</div>
			</fieldset>
		</li>

		<li>
			<fieldset class="layui-elem-field">
				<legend>方法</legend>
				<div class="layui-field-box">
					<select name="action" id="action" class="node-select" SIZE="20"></select>
					<p>
						<button class="layui-btn layui-btn-small" onclick="addNode( '<{:U('Node/add',array('level'=>3,'o'=>'action'))}>' , 'action' ,'添加方法')">添加</button>
						<button class="layui-btn layui-btn-small" onclick="editNode( '<{:U('Node/edit',array('o'=>'action'))}>' , 'action' , '编辑方法')">编辑</button>
						<button class="layui-btn layui-btn-small" onclick="delNode(3)">删除</button>
					</p>
				</div>
			</fieldset>
		</li>
	</div>
</div>
<script type="text/javascript">
$(function(){
	$("#group").change(function(){
		load = parent.layer.load(0,{shade: [0.7, '#000000']});
		var v = $(this).val();
		$.ajax({
			type : "GET",
			url :  "index.php?m=Adminx&c=Node&a=getchild&pid="+v+"&temp="+new Date().getTime(),
			dataType : "json",
			success : function(RES) {
				parent.layer.close(load);
				data = RES;
				var _tmp_html = '';						
				$.each(data, function(x, y) {
					_tmp_html += '<option value="'+y.id+'">'+y.title+'('+y.name+')'+'</option>';
				});
				$('#model').html(_tmp_html);
				$("#action").html('');
			}
		});		
	})

	$("#model").change(function(){
		load = parent.layer.load(0,{shade: [0.7, '#000000']});
		var v = $(this).val();
		$.ajax({
			type : "GET",
			url : "index.php?m=Adminx&c=Node&a=getChild&pid="+v+"&temp="+new Date().getTime(),
			dataType : "json",
			success : function(RES) {
				parent.layer.close(load);
				data = RES;
				var _tmp_html = '';						
				$.each(data, function(x, y) {
					_tmp_html += '<option value="'+y.id+'">'+y.title+'('+y.name+')'+'</option>';
				});
				$('#action').html(_tmp_html);
			}
		});
	})
})

function addNode(url , domid , title){

	if (domid=='model'){
		v= $("#group option:selected").val();
		if (v==undefined) {
			layer.msg("请选择上级分组");
			return false;
		}else{
			url = url + "&pid=" + v;
		}
	}

	if (domid=='action'){
		v= $("#model option:selected").val();
		if (v==undefined) {
			layer.msg("请选择上级控制器");
			return false;
		}else{
			url = url + "&pid=" + v;
		}
	}
	openModel (url,title,'800px','500px')
}

function editNode(url , domid , title){

	if (domid=='group') {
		name = '分组';
		selOpt = $("#group option:selected");	
	}else if(domid=='model'){
		name = '控制器';
		selOpt = $("#model option:selected");
	}else if(domid=='action'){
		name = '方法';
		selOpt = $("#action option:selected");
	}

	v = selOpt.val();
	if(v==undefined){
		alert("请选择要编辑的"+name);
		return false;
	}else{
		url = url + "&id=" + v;
	}

	openModel (url,title,'800px','500px')
}

function delNode(level){
	if (level==1) {
		name = '分组';
		selOpt = $("#group option:selected");	
	}else if(level==2){
		name = '控制器';
		selOpt = $("#model option:selected");
	}else if(level==3){
		name = '方法';
		selOpt = $("#action option:selected");
	}

	v = selOpt.val();
	if(v==undefined){
		layer.msg("请选择要删除的"+name);
		return false;
	}

	load = layer.load(0,{shade: [0.7, '#000000']});
	$.get("index.php?m=Adminx&c=Node&a=del&level="+level+"&nodeID="+v+"&temp="+new Date().getTime(),function(response){
		layer.close(load);
		if (response==1){
			layer.msg('操作成功');
			selOpt.remove();
		}else{
			layer.msg(response);
		} 
	});
 	
}
</script>
